package com.huifan.demo.revenue.hlzs;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.dongliu.requests.RawResponse;
import net.dongliu.requests.Requests;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: liuchenhui
 * @create: 2019-07-12 14:33
 **/
public class HELIZHONGSHUIMain {
    public static final String COMPANY_INFO = "{\"serviceid\":\"HQQYJBXX\",\"nsrsbh\":\"%s\",\"appid\":\"%s\",\"sid\":\"%s\",\"input\":{}}";
    public static final String LOGIN = "{\"serviceid\":\"LOGIN\",\"nsrsbh\":\"%s\",\"appid\":\"%s\",\"sid\":\"%s\",\"input\":{\"mm\":\"%s\"}}";
    public static final String HISTORY = "{\"serviceid\":\"SBJLCX\",\"nsrsbh\":\"%s\",\"appid\":\"%s\",\"sid\":\"%s\",\"input\":{\"skssqq\":\"%s\",\"skssqz\":\"%s\"}}";
    public static final String DETAIL = "{\"serviceid\":\"HQWQSBSJ\",\"nsrsbh\":\"%s\",\"appid\":\"%s\",\"sid\":\"%s\",\"input\":{\"skssyf\":\"%s\"}}";

    public static void main(String[] args) throws UnsupportedEncodingException {
        long time = System.currentTimeMillis();
        String appid = "11d6a2f3-7eb0-9632-a866-8d05ad7a4378";//APPID
        String pwd = "Jrls20151022";
        String sid = System.currentTimeMillis() + "";
        String nsrsbh = "91110105MA0179GM2F";// 税号
        String comInfo = String.format(COMPANY_INFO, nsrsbh, appid, sid);
        String loginInfo = String.format(LOGIN, nsrsbh, appid, sid, pwd);
        String historyInfo = String.format(HISTORY, nsrsbh, appid, sid, "2019-01", "2019-03");
        String detailInfo = String.format(DETAIL, nsrsbh, appid, sid, "2019-05");

        String publickey = "#2*9De0(AC$Def.d";// 联系服务商提供

        String privatekey = "5656398b9cb77f0b";// 联系服务商提供

        String url = "https://itsapi.holytax.com/v3/api/s";// 联系服务商提供

        // String url = "http://127.0.0.1:8002/v2/api/s";// 联系服务商提供
        //
        JSONObject ret = request(url, publickey, privatekey, detailInfo);
        System.out.println("===============解密后的明文===============");
        System.out.println(ret);

        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("时间：" + (System.currentTimeMillis() - time) / 1000);

    }

    public static JSONObject request(String url, String publickey, String privatekey, String json)
            throws UnsupportedEncodingException {
        System.out.println("请求的文明):" + json);
        JSONObject requestjsonobj = JSON.parseObject(json);
        String inputstr = requestjsonobj.getJSONObject("input").toJSONString();
        System.out.println(inputstr);
        String inputmi = AESUtil.encrypt(inputstr, privatekey);
        System.out.println("input内容加密:" + inputmi);
        requestjsonobj.put("input", inputmi);
        String requestjson = AESUtil.encrypt(requestjsonobj.toJSONString(), publickey);
        System.out.println("报文加密:" + requestjson);
        Map<String, Object> reqbody = new HashMap<String, Object>();
        reqbody.put("json", URLEncoder.encode(requestjson, "UTF-8"));

        RawResponse response = Requests.post(url).body(reqbody).verify(false).timeout(60000).send();
        String responsetext = response.readToText();

        System.out.println("接受返回报文：" + responsetext);

        JSONObject ret = JSON.parseObject(responsetext);
        String rawdata = ret.getString("data");
        if (rawdata != null && !rawdata.equals("")) {
            System.out.println(rawdata);
            String outdata = AESUtil.decrypt(rawdata, privatekey);
            System.out.println(outdata);
            Object jsonobj = JSON.parse(outdata);
            ret.put("data", jsonobj);
        }
        return ret;
    }
}
